package com.lancoo.reviewsystem.utils;

import android.content.Context;
import android.widget.Toast;

/**
 * Created by tangminglong on 17/4/19.
 * 避免同样的信息多次触发重复弹出的问题
 */

public class ToastUtils {


    private static Context sContext;
    private static String oldMsg;
    protected static Toast toast = null;
    private static long oneTime = 0;
    private static long twoTime = 0;
    private static int duration = Toast.LENGTH_SHORT;

    private ToastUtils()
    {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }


    public static void init(Context context){
        sContext = context;
    }

    /**
     * 短时间显示Toast
     *
     * @param message
     */
    public static void showShort(String message)
    {
        if (toast == null) {
            toast = Toast.makeText(sContext, message, Toast.LENGTH_SHORT);
            toast.show();
            oneTime = System.currentTimeMillis();
        }else {
            twoTime = System.currentTimeMillis();
            duration = Toast.LENGTH_SHORT;
            toast.setDuration(duration);
            if (message.equals(oldMsg)){
                if (twoTime - oneTime > duration){
                    toast.show();
                }
            }else {
                oldMsg = message;
                toast.setText(message);
                toast.show();
            }
            oneTime = twoTime;

        }
    }


    /**
     * 长时间显示Toast
     *
     * @param message
     */
    public static void showLong(String message)
    {
        if (toast == null) {
            toast = Toast.makeText(sContext, message, Toast.LENGTH_LONG);
            toast.show();
            oneTime = System.currentTimeMillis();
        }else {
            twoTime = System.currentTimeMillis();
            duration = Toast.LENGTH_LONG;
            toast.setDuration(duration);
            if (message.equals(oldMsg)){
                if (twoTime - oneTime > duration){
                    toast.show();
                }
            }else {
                oldMsg = message;
                toast.setText(message);
                toast.show();
            }
            oneTime = twoTime;

        }
    }






}
